Automatical start containers
참고: https://docs.docker.com/engine/admin/host_integration/
시스템 재부팅 후에 docker 서비스가 올라가거나, docker에서 컨테이너가 분리(detach)되었을때 재시작을 하는 방법입니다.
2016-10-11일 기준으로 docker-engine의 버전은 1.12.2입니다.
- detach 된 컨테이너의 자동 실행:
docker start -a [컨테이너명]
-a 옵션으로 attach 하도록 합니다.
부팅시 컨테이너 자동 시작
부팅시에는 systemd에 서비스 스크립트를 적용해야 합니다.
[Unit]
Description=Redis container
Requires=docker.service
After=docker.service
[Service]
Restart=always
ExecStart=/usr/bin/docker start -a redis_server
ExecStop=/usr/bin/docker stop -t 2 redis_server
[Install]
WantedBy=default.target
If you intend to use this as a system service, put the above contents in a file in the /etc/systemd/system directory, e.g. /etc/systemd/system/docker-redis_server.service.
If you need to pass options to the redis container (such as --env), then you’ll need to use docker run rather than docker start. This will create a new container every time the service is started, which will be stopped and removed when the service is stopped.
[Service]
...
ExecStart=/usr/bin/docker run --env foo=bar --name redis_server redis
ExecStop=/usr/bin/docker stop -t 2 redis_server
ExecStopPost=/usr/bin/docker rm -f redis_server
...
To start using the service, reload systemd and start the service:
systemctl daemon-reload
systemctl start docker-redis_server.service
To enable the service at system startup, execute:
systemctl enable docker-redis_server.service